ige.controller('TaxSettingController', ['$scope', 'TaxSettingService', '$mdToast', '$state', 'EventBus', '$timeout',
    function ($scope, TaxSettingService, $mdToast, $state, EventBus, $timeout) {
        var pageSkip = 0;
        var currentDeleteId = 0;
        var deleteArray = [];

        var timer;
        $scope.noNewData = false;
        EventBus.off("TaxSetting.keywordSearch");
        EventBus.on("TaxSetting.keywordSearch", function (event) {
            if (event.data.keyword) {
                $timeout.cancel(timer);
                timer = $timeout(
                    function () {
                        pageSkip = 0;
                        $scope.TaxSettings = [];
                        $scope.filter = event.data;
                        $scope.getTaxSettings();
                    }, 300
                );
            }
        });

        $scope.$on(
            "$destroy",
            function (event) {
                $timeout.cancel(timer);
            }
        );

        $scope.TaxSettings = [];
        $scope.loadMore = false;
        $scope.loaddingListData = true;
        $scope.getTaxSettings = function () {
            $scope.loaddingListData = true;
            TaxSettingService.getTaxSettings(pageSkip, ige.systemSetting.defaultPageSize, $scope.filter, function (data) {
                $scope.TaxSettings = $scope.TaxSettings.concat(data);
                if (data.length == 0) {
                    $scope.noNewData = true;
                } else {
                    $scope.noNewData = false;
                }
            }).then(function () {
                pageSkip += ige.systemSetting.defaultPageSize;
            }).then(function () {
                $scope.loaddingListData = false;
            });
        };

        $scope.loadMoreData = function () {
            $scope.loadMore = true;
            TaxSettingService.getTaxSettings(pageSkip, ige.systemSetting.defaultPageSize, $scope.filter, function (data) {
                $scope.TaxSettings = $scope.TaxSettings.concat(data);
                if (data.length == 0) {
                    $scope.noNewData = true;
                } else {
                    $scope.noNewData = false;
                }
            }).then(function () {
                pageSkip += ige.systemSetting.defaultPageSize;
            }).then(function () {
                $scope.loadMore = false;
            });
        };

        $scope.edit = function (id) {
            $state.go('MainNav.EditTaxSetting', {id: id});
        };
        $scope.getTaxSettings();
        $scope.itemHide = function (id) {
            return deleteArray.indexOf(id) > -1;
        };

        $scope.loadding = function (id) {
            return id == currentDeleteId;
        };
        var deleteSuccess = false;
        $scope.delete = function (id) {
            currentDeleteId = id;
            TaxSettingService.deleteTaxSetting(id, function (data) {
                deleteSuccess = true;
            }).then(function () {
                if (deleteSuccess) {
                    currentDeleteId = 0;
                    deleteArray.push(id);
                }
                deleteSuccess = false;
            });
        }
    }]);


ige.controller('AddTaxSettingController', ['$scope', 'TaxSettingService', '$mdToast', '$state',
    function ($scope, TaxSettingService, $mdToast, $state) {
        $scope.loadding = false;
        $scope.Departments = null;
        $scope.currentUpBound = 100;
        $scope.currentLowBound = 0;
        $scope.currentRate = 0.00;
        $scope.loadding = false;
        var addSuccess = false;


        $scope.currentIndex = 1;
        $scope.TaxSetting = {
            Name: undefined,
            Description: undefined,
            Rules: []
        };

        $scope.deleteTaxItem = function () {
            var tempTax = $scope.TaxSetting.Rules.pop();
            $scope.currentLowBound = tempTax.Lowbound;
            $scope.currentUpBound = tempTax.Upbound;
            $scope.currentIndex--;
        };

        $scope.addTaxItem = function () {

            $scope.TaxSetting.Rules.push({
                Index: $scope.currentIndex,
                Lowbound: $scope.currentLowBound,
                Upbound: $scope.currentUpBound,
                Rate: $scope.currentRate
            });

            $scope.currentLowBound = $scope.currentUpBound;
            $scope.currentUpBound = $scope.currentLowBound + 1;
            $scope.currentRate = 0;
            $scope.currentIndex++;

        };


        $scope.addTaxSetting = function () {
            //console.log($scope.TaxSetting);
            //return;
            $scope.loadding = true;
            TaxSettingService.addTaxSetting($scope.TaxSetting, function (data) {
                addSuccess = true;
            }).then(function () {
                if (addSuccess) {
                    $state.go('MainNav.TaxSetting');
                } else {
                }
                $scope.loadding = false;
                addSuccess = false;
            });
        };
    }]);

ige.controller('EditTaxSettingController', ['$scope', 'TaxSettingService', '$mdToast', '$state', '$stateParams',
    function ($scope, TaxSettingService, $mdToast, $state, $stateParams) {
        var id = $stateParams.id;
        $scope.loadding = false;
        $scope.currentUpBound = 100;
        $scope.currentLowBound = 0;
        $scope.currentRate = 0.00;
        $scope.currentIndex = 1;
        var editSuccess = false;
        $scope.TaxSetting = {
            Id: id,
            Name: undefined,
            Description: undefined,
            Rules: []
        };

        var currentItem = null;
        TaxSettingService.getTaxSetting(id, function (data) {
            console.log(data);
            $scope.TaxSetting.Name = data.name;
            $scope.TaxSetting.Description = data.description;
            for (var inx = 0; inx < data.rules.length; inx++) {
                if ($scope.currentIndex < data.rules[inx].index) {
                    $scope.currentIndex = data.rules[inx].index;
                    currentItem = data.rules[inx];
                }
                $scope.TaxSetting.Rules.push(
                    {
                        Index: parseInt(data.rules[inx].index),
                        Lowbound: parseFloat(data.rules[inx].lowbound),
                        Upbound: parseFloat(data.rules[inx].upbound),
                        Rate: parseFloat(data.rules[inx].rate)
                    }
                );
            }
            $scope.currentIndex++;
            $scope.currentLowBound = parseInt(currentItem.upbound) + 1;
            $scope.currentUpBound = $scope.currentLowBound + 1;
            $scope.currentRate = parseFloat(currentItem.rate);
        });


        $scope.deleteTaxItem = function () {
            var tempTax = $scope.TaxSetting.Rules.pop();

            $scope.currentLowBound = parseFloat(tempTax.Lowbound);
            $scope.currentUpBound = parseFloat(tempTax.Upbound);
            $scope.currentIndex--;
            console.log($scope.currentIndex);
        };


        $scope.addTaxItem = function () {
            $scope.TaxSetting.Rules.push({
                Index: $scope.currentIndex,
                Lowbound: $scope.currentLowBound,
                Upbound: $scope.currentUpBound,
                Rate: $scope.currentRate
            });

            $scope.currentLowBound = $scope.currentUpBound;
            $scope.currentUpBound = $scope.currentLowBound + 1;
            $scope.currentRate = 0;

            $scope.currentIndex++;
        };


        console.log($scope.TaxSetting);


        $scope.editTaxSetting = function () {
            //console.log($scope.TaxSetting);
            //return;
            $scope.loadding = true;
            TaxSettingService.editTaxSetting($scope.TaxSetting, function (data) {
                editSuccess = true;
            }).then(function () {
                if (editSuccess) {
                    $state.go('MainNav.TaxSetting');
                } else {
                }
                $scope.loadding = false;
                editSuccess = false;
            });
        };
    }]);

ige.controller('RewardShowController', ['$scope', 'TaxSettingService', '$mdToast', '$state', '$stateParams', 'TaxSettingService', '$mdDialog',
    function ($scope, TaxSettingService, $mdToast, $state, $stateParams, TaxSettingService, $mdDialog) {
        $scope.RewardMethods = null;
        $scope.RewardMethodId = null;
        $scope.ResultMethodId = 0;
        $scope.ResultMethods = [
            {
                Name: '全部', Id: 0
            },
            {
                Name: '工资最多', Id: 1
            },
            {
                Name: '奖金最多', Id: 2
            },
            {
                Name: '随机', Id: 3
            }];

        $scope.RewardAvgs = [
            {Name: '单月', Id: 0},
            {Name: '12月平摊', Id: 1}
        ];

        $scope.RewardAvgId = 0;

        TaxSettingService.getTaxSettings(null, null, null, function (data) {
            $scope.RewardMethods = data;
        });

        $scope.closeDialog = function () {
            $mdDialog.cancel();
        };
        $scope.answer = function (rewardMethodId, resultMethodId, rewardAvgId) {
            $mdDialog.hide({rewardMethodId: rewardMethodId, resultMethodId: resultMethodId, RewardAvgId: rewardAvgId});
        };
    }]);